package com.chandra.pos.customer.repository;

import com.chandra.pos.SearchCriteria;
import com.chandra.pos.customer.dao.entity.Customer;
import com.chandra.pos.customer.model.CustomerSearchCriteria;
import org.primefaces.model.SortOrder;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Admin
 * Date: 11/15/13
 * Time: 7:53 PM
 * To change this template use File | Settings | File Templates.
 */

public class CustomerRepositoryImpl implements CustomerRepositoryCustom {

    @PersistenceContext
    private EntityManager entityManager;


    @Override
      public List<Customer> search(CustomerSearchCriteria searchCriteria) {




        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Customer> criteriaQuery=criteriaBuilder.createQuery(Customer.class) ;

        Root<Customer> userRoot = criteriaQuery.from(Customer.class);


        if (searchCriteria.getSortBy() != null && searchCriteria.getSortBy() != null) {
            if (searchCriteria.getSortOrder().equals(SortOrder.ASCENDING))
                criteriaQuery.orderBy(criteriaBuilder.asc(userRoot.get(searchCriteria.getSortBy())));
            else
                criteriaQuery.orderBy(criteriaBuilder.desc(userRoot.get(searchCriteria.getSortBy())));
        }


        List<Predicate> predicates = new ArrayList<Predicate>();


       if(searchCriteria.getFilters() !=null){
        for (final Map.Entry<String, String> e : searchCriteria.getFilters().entrySet())
        {

            final String key = e.getKey();
            final String value = e.getValue();

            if ((key != null) && (value != null)) {

                if (value.contains("%")) {
                    predicates.add(criteriaBuilder.like(userRoot.<String> get(key), value));
                } else {
                    predicates.add(criteriaBuilder.equal(userRoot.get(key), value));
                }
            }
        }
       }else{
            if (searchCriteria.getFirstName() != null)
            {

                predicates.add(criteriaBuilder.equal(userRoot.get("firstName"),searchCriteria.getFirstName()));

            }

            if (searchCriteria.getLastName() != null)
                predicates.add(criteriaBuilder.equal(userRoot.get("lastName"),searchCriteria.getLastName()));


            if (searchCriteria.getCity() != null)
                predicates.add(criteriaBuilder.equal(userRoot.get("city"),searchCriteria.getCity()));



            if (searchCriteria.getNickName() != null)
                predicates.add(criteriaBuilder.equal(userRoot.get("nickName"),searchCriteria.getNickName()));



            if (searchCriteria.getEmail() != null)
                predicates.add(criteriaBuilder.equal(userRoot.get("email"),searchCriteria.getEmail()));


            if (searchCriteria.getAddress() != null)
                predicates.add(criteriaBuilder.equal(userRoot.get("address"),searchCriteria.getAddress()));


            if (searchCriteria.getCountry() != null)
                predicates.add(criteriaBuilder.equal(userRoot.get("country"),searchCriteria.getCountry()));


            if (searchCriteria.getPrimaryCell() != null)
                predicates.add(criteriaBuilder.equal(userRoot.get("primaryCell"),searchCriteria.getPrimaryCell()));


            if (searchCriteria.getSecondaryCell() != null)
                predicates.add(criteriaBuilder.equal(userRoot.get("secondaryCell"),searchCriteria.getSecondaryCell()));



            if (searchCriteria.getZipCode() != null)
                predicates.add(criteriaBuilder.equal(userRoot.get("zipCode"),searchCriteria.getZipCode()));


        }



        criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));
        criteriaQuery.distinct(true);

        return entityManager.createQuery(criteriaQuery).getResultList();


    }
    @Override
    public List<Customer> search(SearchCriteria searchCriteria) {


        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Customer> criteriaQuery=criteriaBuilder.createQuery(Customer.class) ;

        Root<Customer> userRoot = criteriaQuery.from(Customer.class);

        List<Predicate> predicates = new ArrayList<Predicate>();






        criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));
        criteriaQuery.distinct(true);

        return entityManager.createQuery(criteriaQuery).setFirstResult(searchCriteria.getFirst()).setMaxResults(searchCriteria.getPageSize()).getResultList();



    }
}
